// pages/index/serviceList/index.js
import Dialog from '../../../../miniprogram_npm/@vant/weapp/dialog/dialog';
import Toast from '../../../../miniprogram_npm/@vant/weapp/toast/toast';
const $api = require('../../../../request/api').API;

const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    serviceList: [],
    localeId: '',
    showTagOverlay: false,
    guanxiList: ['父亲', '母亲', '子女', '老公', '老婆', '哥哥', '弟弟', '妹妹', '亲戚', '朋友', '同事', '恋人'],
    guanxiName: '',
    guanxiIndex: 99,
    customerId: '',
    btnType: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let id = options.id;
    console.log(id);
    if (id != undefined) {
      this.setData({
        localeId: id
      });
    }
  },
  // 打开新增tag弹框
  openTagOverlay(e) {
    let customerId = e.currentTarget.dataset.id;
    let type = e.currentTarget.dataset.type;
    this.setData({
      showTagOverlay: true,
      customerId: customerId,
      guanxiIndex: 99,
      guanxiName: '',
      btnType: type
    });
  },
  // 关闭新增tag弹框
  closeTagOverlay() {
    this.setData({
      showTagOverlay: false
    });
  },
  // 选择关系
  clickGx(e) {
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    if (index == this.data.guanxiIndex) {
      this.setData({
        guanxiIndex: 99,
        guanxiName: ''
      });
    } else {
      this.setData({
        guanxiIndex: index,
        guanxiName: item
      });
    }
    console.log('data--', this.data.guanxiIndex, this.data.guanxiName);
  },
  // 列表数据
  getData() {
    let params = {
      localeId: this.data.localeId
    };
    $api.listCustomerByLocaleId(params).then(res => {
      this.setData({
        serviceList: res.data.data
      });
      console.log('用户列表', this.data.serviceList);
    });
  },
  tagNameInput(e) {
    let value = e.detail.value;
    this.setData({
      guanxiName: value
    });
  },
  // 解绑
  jiebang(e) {
    let id = e.currentTarget.dataset.id;
    Dialog.confirm({
      title: '解绑',
      message: '是否解绑该用户？'
    })
      .then(() => {
        let params = {
          customerId: id,
          localeId: this.data.localeId,
          type: 2
        };
        $api.customerOperation(params).then(res => {
          if (res.data.status == 0) {
            wx.showToast({
              title: '解绑成功'
            });
            setTimeout(() => {
              this.getData();
            }, 1000);
          }
        });
      })
      .catch(() => {
        wx.showToast({
          title: '取消解绑'
        });
      });
  },
  // 转移
  zhuanyi(e) {
    let id = e.currentTarget.dataset.id;
    Dialog.confirm({
      title: '转移',
      message: '是否转移权限给该用户？'
    })
      .then(() => {
        let params = {
          customerId: id,
          localeId: this.data.localeId,
          type: 3
        };
        $api.customerMakeOver(params).then(res => {
          if (res.data.status == 0) {
            wx.showToast({
              title: '权限转移成功'
            });
            setTimeout(() => {
              wx.switchTab({
                url: '/pages/equipment/index'
              });
            }, 1000);
          }
        });
      })
      .catch(() => {
        wx.showToast({
          title: '取消转移'
        });
      });
  },
  // 设置主用户
  setZhu(e) {
    let id = e.currentTarget.dataset.id;
    Dialog.confirm({
      title: '提示',
      message: '是否将该用户设置为主用户'
    }).then(res => {
      let params = {
        customerId: id,
        localeId: this.data.localeId,
        type: 3
      };
      $api.customerOperation(params).then(res => {
        if (res.data.status == 0) {
          wx.showToast({
            title: '设置成功'
          });
          setTimeout(() => {
            this.getData();
          }, 1000);
        }
      });
    });
  },
  // 提交
  addTag() {
    let relative = this.data.guanxiName;
    let index = this.data.guanxiIndex;
    let btnType = this.data.btnType;
    if (relative == '' || index == 99) {
      Toast('请设置关系');
      return;
    }
    if (btnType == '1') {
      //授权
      let params = {
        customerId: this.data.customerId,
        localeId: this.data.localeId,
        type: 1,
        relative: relative
      };
      console.log('授权data', params);

      $api.customerOperation(params).then(res => {
        if (res.data.status == 0) {
          wx.showToast({
            title: '授权成功'
          });
          this.setData({
            showTagOverlay: false
          });
          setTimeout(() => {
            this.getData();
          }, 1000);
        }
      });
    }
    if (btnType == '2') {
      // 设置关系
      let params = {
        localeId: this.data.localeId,
        customerId: this.data.customerId,
        relative: relative
      };
      console.log('设关系data', params);

      $api.setCustomerRelative(params).then(res => {
        if (res.data.status == 0) {
          wx.showToast({
            title: '设置关系成功'
          });
          this.setData({
            showTagOverlay: false
          });
          setTimeout(() => {
            this.getData();
          }, 1000);
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
});
